<?php


use yii\bootstrap\BootstrapAsset;
use yii\helpers\Url;
use yii\helpers\Html;
use common\models\business\BusinessFarmers;
use common\models\business\BusinessException;
use common\models\business\BusinessPig;
BootstrapAsset::register($this);

$sql = 'select id,no,headperson,canlivery,price,cost,headpersonid,planout,status,intime from '.BusinessPig::tableName().' where livery=:id';
$pigs = Yii::$app->db->createCommand($sql)->bindValues([':id'=>$model->id])->queryAll();


?>




<div class="row">
    <div class="col-md-12">
        <table class="table table-hover">
            <tr>
                <th>代养仔猪编号</th>
                <th>入栏时间</th>
                <th>巡检负责人</th>
                <th>是否可以领养</th>
                <th>领养价格</th>
                <th>成本价</th>
                <th>状态</th>
                <th>操作</th>
            </tr>
            <?php foreach ($pigs as $key => $row) {?>
                <tr>
                    <td><a target="_blank" href="<?= Url::to(['/business/pig/view','id'=>$row['id']])?>"><?= $row['no'] ?></a></td>
                    <td><?= $row['intime']!=0?date('Y-m-d',$row['intime']):'未设置' ?></td>
                    <td><a target="_blank" href="<?= Url::to(['/business/person/view','id'=>$row['headpersonid']])?>"> <?= $row['headperson'] ?></a></td>
                    <td><?= BusinessPig::$canlivery[$row['canlivery']] ?></td>
                    <td><?= $row['price'] ?></td>
                    <td><?= $row['cost'] ?></td>
                    <td>
                        <?php
                            $outdays = \common\models\sys\SysSetting::getValue('waitout');

                            if($row['status']==BusinessPig::STATUS_EXCEPTION){
                                $exception = BusinessException::find()->select('reason')->where('pigid=:id',[':id'=>$model->id])->asArray()->one();
                                echo Html::label(BusinessPig::$statues[$row['status']], '', ['class' => 'btn btn-xs btn-danger','data-id'=>$model->id,'data-toggle'=>'tooltip','data-placement'=>'top','title'=>$exception['reason']]);
                            }else if($row['planout']-time() <= $outdays*3600*24 && !empty($model->planout)){
                                echo Html::label('预出栏', '', ['class' => 'btn btn-xs','style'=>'background:orange']);
                            }else if($row['status']==BusinessPig::STATUS_BRFOREIN) {
                                echo Html::label(BusinessPig::$statues[$row['status']], '', ['class' => 'btn btn-xs btn-default']);
                            }else if($row['status']==BusinessPig::STATUS_AFTERIN){
                                echo Html::label(BusinessPig::$statues[$row['status']], '', ['class' => 'btn btn-xs btn-primary']);
                            }else if($row['status']==BusinessPig::STATUS_WAITOUT){
                                echo Html::label(BusinessPig::$statues[$row['status']], '', ['class' => 'btn btn-xs btn-info']);
                            }else if($row['status']==BusinessPig::STATUS_WAITKILL){
                                echo Html::label(BusinessPig::$statues[$row['status']], '', ['class' => 'btn btn-xs btn-primary']);
                            }else if($row['status']==BusinessPig::STATUS_KILLED){
                                echo Html::label(BusinessPig::$statues[$row['status']], '', ['class' => 'btn btn-xs btn-success']);
                                return;
                            }else{
                                echo Html::label(BusinessPig::$statues[$row['status']], '', ['class' => 'btn btn-xs btn-default']);
                            }
                        ?>
                    </td>
                    <td><a target="_blank" href="<?= Url::to(['/business/pig/view','id'=>$row['id']])?>">查看</a> | <a target="_blank" href="<?= Url::to(['/business/pig/update','id'=>$row['id']])?>">修改</a></td>


                </tr>
            <?php } ?>
        </table>
    </div>
</div>